use rand::Rng;
use std::cmp::Ordering;
use std::io;

fn main() {
    println!("请猜一个数字");
    let secret_number = rand::rng().random_range(1..=100);

    loop {
        println!("请输入你猜的数字");
        let mut guess = String::new();

        io::stdin().read_line(&mut guess).expect("读取数字失败");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };
        println!("你猜的数字: {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("数字小了"),
            Ordering::Greater => println!("数字大了"),
            Ordering::Equal => {
                println!("恭喜，猜对了");
                break;
            }
        }
    }
}
